package com.mjh.zhujianju.service.party.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.mjh.zhujianju.vo.agency.DeptUserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.PageHelper;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.utils.XTEntityUtils;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.dao.party.BuildingNewsMapper;
import com.mjh.zhujianju.enums.BuildingNewsEnum;
import com.mjh.zhujianju.enums.BusinessEnum;
import com.mjh.zhujianju.model.agency.DepartmentEO;
import com.mjh.zhujianju.model.agency.DeptUserEO;
import com.mjh.zhujianju.model.agency.OrganizationEO;
import com.mjh.zhujianju.model.base.ContentEO;
import com.mjh.zhujianju.model.party.BuildingNewsEO;
import com.mjh.zhujianju.service.agency.DepartmentService;
import com.mjh.zhujianju.service.agency.DeptUserService;
import com.mjh.zhujianju.service.agency.OrganizationService;
import com.mjh.zhujianju.service.base.ContentService;
import com.mjh.zhujianju.service.base.HitsService;
import com.mjh.zhujianju.service.party.BuildingNewsService;
import com.mjh.zhujianju.util.FileUrlAdapter;
import com.mjh.zhujianju.vo.party.BuildingNewsVO;

/**
 * @author liuyang
 * @Description: 党建
 * @date 2018/8/15 0:10
 */
@Service
public class BuildingNewsServiceImpl extends BaseServiceImpl<BuildingNewsEO> implements BuildingNewsService {

    @Autowired
    private BuildingNewsMapper buildingNewsMapper;

    @Autowired
    private FileService fileService;

    @Autowired
    private ContentService contentService;

    @Autowired
    private HitsService hitsService;

    @Autowired
    private DeptUserService deptUserService;

    @Autowired
    private DepartmentService departmentService;
    
    @Autowired
    private OrganizationService organizationService;

    public BuildingNewsServiceImpl(BuildingNewsMapper buildingNewsMapper) {
        super(buildingNewsMapper);
    }

    @Override
    public List<BuildingNewsVO> queryBuildingNewsIndexList() throws XTBusinessException {
        return queryList(4L,null);
    }

    @Override
    public List<BuildingNewsVO> queryBuildingNewsList(String type) throws XTBusinessException {
        return queryList(null,type);
    }

    @Override
    public BuildingNewsVO queryBuildingNewsInfo(Long id) throws XTBusinessException {
        BuildingNewsVO buildingNewsVO = new BuildingNewsVO();
        BuildingNewsEO buildingNewsEO = buildingNewsMapper.selectByPrimaryKey(id);
        if (buildingNewsEO == null){
            return buildingNewsVO;
        }

        BeanUtils.copyProperties(buildingNewsEO,buildingNewsVO);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        buildingNewsVO.setCreateTimeStr(sdf.format(buildingNewsVO.getCreateTime()));

        //查询发布部门
        DeptUserEO deptUserEO = deptUserService.getDeptByUserId(buildingNewsVO.getCreateUserId());
        if(deptUserEO != null){
            DepartmentEO departmentEO = departmentService.getByPrimaryKey(deptUserEO.getDeptId());
            if(departmentEO != null){
                buildingNewsVO.setDepartmentName(departmentEO.getName());
            }
        }

        String type = BusinessEnum.TYPE_BUILDING.getCode();

        //查询内容
        ContentEO contentEO = contentService.queryContentByTypeAndId(type,id);
        if (contentEO != null){
            buildingNewsVO.setBasicFunction(contentEO.getContent());
        }

        //添加浏览量
        hitsService.updateNumber(type,id);

        //阅读数量
        Long hitsTotal = hitsService.queryHitsTotal(type,id);
        buildingNewsVO.setTotalHits(hitsTotal);
        return buildingNewsVO;
    }

    @Override
    public PagedResult<BuildingNewsVO> findBuildingNewsPagination(BuildingNewsEO buildingNewsEO) {
        Integer pageNo = buildingNewsEO.getPageNo() == null ? 1 : buildingNewsEO.getPageNo();
        Integer pageSize = buildingNewsEO.getPageSize() == null ? 15 : buildingNewsEO.getPageSize();
        PageHelper.startPage(pageNo, pageSize);
        List<BuildingNewsEO> list = buildingNewsMapper.selectPagination(buildingNewsEO);
        PagedResult<BuildingNewsEO> page = PagedBeanUtil.toPagedResult(list);
        PagedResult<BuildingNewsVO> result = new PagedResult<BuildingNewsVO>();
        BeanUtils.copyProperties(page, result);
        if (!XTListUtils.isEmpty(list)) {
            List<Long> ids = XTEntityUtils.getIdList(list);
            List<BuildingNewsVO> voList = new ArrayList<BuildingNewsVO>();
            Map<Long, FileEO> files = fileService.getMapTopOne(ids, BuildingNewsEO.class.getName(), null);
            for (BuildingNewsEO buildingNewsEO1 : list) {
                BuildingNewsVO d = new BuildingNewsVO();
                BeanUtils.copyProperties(buildingNewsEO1, d);
                d.setType(BuildingNewsEnum.getName(d.getType()));
                voList.add(d);
                FileEO file = files.get(d.getId());
                if (file != null) {
                    d.getFiles().add(file.getId());
                }
            }
            result.setDataList(voList);
        }
        return result;
    }

    private String getContent(Long id) {
        ContentEO content = contentService.queryContentByTypeAndId(BusinessEnum.TYPE_BUILDING.getCode(), id);
        if (content != null) {
            return content.getContent();
        }
        return null;
    }

    @Override
    public BuildingNewsVO getInfo(Long id) throws XTBusinessException {
        BuildingNewsEO buildingNewsEO = super.getByPrimaryKey(id);
        if (buildingNewsEO != null) {
            BuildingNewsVO vo = new BuildingNewsVO();
            BeanUtils.copyProperties(buildingNewsEO, vo);
            vo.setBasicFunction(getContent(id));
            List<FileEO> files = fileService.getFileById(id, BuildingNewsEO.class.getName(), null);
            if(!XTListUtils.isEmpty(files)) {
                vo.setFiles(XTEntityUtils.getIdList(files));
            }
            vo.setFileEOList(files);
            return vo;
        }

        return null;
    }

    private List<BuildingNewsVO> queryList(Long limit,String type) throws XTBusinessException {
        List<BuildingNewsVO> list = buildingNewsMapper.querybuildingNewsList(type);
        if (CollectionUtils.isEmpty(list)){
            return null;
        }

        if (limit != null){
            list = list.stream().limit(limit).collect(Collectors.toList());
        }

        List<Long> caseIds = list.stream().map(buildingNewsEO1 -> buildingNewsEO1.getId()).distinct().collect(Collectors.toList());
        List<FileEO> fileList = fileService.getFileByIds(caseIds,BuildingNewsEO.class.getName(),null);
        if (CollectionUtils.isEmpty(fileList)){
            return list;
        }

        //查询发布部门
        for(BuildingNewsVO newsVO : list){
            DeptUserEO deptUserEO = deptUserService.getDeptByUserId(newsVO.getCreateUserId());
            if(deptUserEO != null){
                DepartmentEO departmentEO = departmentService.getByPrimaryKey(deptUserEO.getDeptId());
                if(departmentEO != null){
                    newsVO.setDepartmentName(departmentEO.getName());
                }
            }
        }

        for(BuildingNewsVO newsVO : list){
            for (FileEO fileEO : fileList){
                if (newsVO.getId().equals(fileEO.getCaseId())){
                    newsVO.setFileAction(FileUrlAdapter.assemblyAction(fileEO.getId()));
                    continue;
                }
            }
        }
        return list;
    }
}
